<?php

namespace addons\shopro\controller;

use addons\shopro\exception\Exception;
use addons\shopro\model\Config;
use addons\shopro\model\Recharge;
use addons\shopro\model\Store as ModelStore;
use addons\shopro\model\User;
use addons\shopro\model\UserStore;
use app\admin\model\shopro\store\Category;

class Store extends Base
{

    protected $noNeedLogin = ['getgoods','index','category','getlist','getInfo','getMoneyLogList'];
    protected $noNeedRight = ['*'];


    public function index()
    {
        $user = User::info();
        $userStore = UserStore::where('user_id', $user->id)->select();
        $store_id_arr = array_column($userStore, 'store_id');

        $stores = [];
        if ($store_id_arr) {
            $stores = ModelStore::show()->where('id', 'in', $store_id_arr)->select();
        }

        $this->success('获取门店列表', $stores);
    }
    public function category(){
        $storeCategoryModel = new Category();
        $pid = $storeCategoryModel->where(['pid'=>0])
            ->order('weigh','desc')->value('id');
        $categoryList = $storeCategoryModel->where(['pid'=>$pid,'status'=>'normal'])->order('weigh','desc')->select();
        $this->success('',$categoryList);
    }
    public function getlist(){
        $params = $this->request->get();
        $where = '';
        if (!empty($params['category_id'])){
            $category_id = $params['category_id'];
            $where = "FIND_IN_SET($category_id,category_ids)";
        }
        $storeModel = new \addons\shopro\model\Store();
        $storeList = $storeModel
            ->where(['status'=>1,'is_tui'=>1])
            ->where($where)
            ->order('sort','desc')
            ->paginate(10);
        $scoreRatio = 0;
        $scoreConfig = Config::where('name', 'score')->find();
        $scoreConfig = json_decode($scoreConfig['value'], true);
        if(!empty($scoreConfig['consume_get_score']) && !empty($scoreConfig['consume_get_score_ratio'])) {
            $scoreRatio = intval($scoreConfig['consume_get_score_ratio']);
        }
        foreach ($storeList as $key => &$value){
            if($value['consume_get_score'] == 0){
                $value['consume_get_score_ratio'] = $scoreRatio;
            }
        }
        $this->success('',$storeList);
    }

    public function getInfo(){
        $params = $this->request->get();
        if (empty($params['id'])){
            $this->error('请核对参数');
        }
        $storeModel = new \addons\shopro\model\Store();
        $storeList = $storeModel
            ->where(['id'=>$params['id']])

            ->select();
        $this->success('',$storeList);
    }

    public function getgoods(){
        $params = $this->request->get();
        if (empty($params['store_id'])){
            $this->error("请选择门店");
        }
        $goodsModel = new \addons\shopro\model\Goods();
        $goodslist = $goodsModel->where(['store_id'=>$params['store_id'],'status'=>'up'])->paginate(10);
        $this->success('',$goodslist);
    }

    public function getMoneyLogList(){
        $params = input('get.');
        $date_type = $params['date_type'] ?? 'today';
        $store_id = input('store_id');
        $date = $params['date'] ?? '';
        $date = str_replace('[','',$date);
        $date = str_replace(']','',$date);
        $date = str_ireplace('&quot;','',$date);
        $date = explode(',', $date);
        if(empty($store_id)){
            $this->error("请选择门店");
        }
        log_print($date_type);
        $type = input('type');

            switch ($date_type) {
                case 'today':
                    $start = strtotime(date('Y-m-d'));
                    $end = strtotime(date('Y-m-d', (time() + 86400)));
                    break;
                case 'yesterday':
                    $start = strtotime(date('Y-m-d', (time() - 86400)));
                    $end = strtotime(date('Y-m-d'));
                    break;
                case 'week':
                    // $start = strtotime(date('Y-m-d', (time() - (86400 * 6))));  // 减 6 天
                    $start = strtotime(date('Y-m-d', strtotime("-1 week Monday")));     // 周一开始
                    $end = strtotime(date('Y-m-d', (time() + 86400)));          // 到今天结束
                    break;
                case 'month':
                    // $start = strtotime(date('Y-m-d', (time() - (86400 * 30))));  // 减 30 天
                    $start = strtotime(date('Y-m-d', mktime(0, 0, 0, date("m"), 1, date("Y"))));  // 减 30 天
                    $end = strtotime(date('Y-m-d', (time() + 86400)));          // 到今天结束
                    break;
                case 'custom':
                    $date_start = $date[0] ?? date('Y-m-d');        // 默认查 today
                    $date_end = $date[1] ?? date('Y-m-d');          // 默认查 today
                    $start = strtotime($date_start);
                    $end = strtotime($date_end) + 86400;
                    break;
                default:
                    $start = strtotime(date('Y-m-d'));
                    $end = strtotime(date('Y-m-d', (time() + 86400)));
                    break;
            }

        $typewhere = [];
        if(!empty($type)){
            $typewhere = ['type'=>$type];
        }
        $limit = input('limit/d',10);
        $rechargeModel = new Recharge();
        $zhichu = $rechargeModel->where(['store_id'=>$store_id])
            ->where(['type'=>'zhichu'])
            ->whereBetween('create_time',[$start,$end])
            ->sum('money');
        $shouru = $rechargeModel
            ->where(['store_id'=>$store_id])
            ->where(['type'=>'shouru'])
            ->whereBetween('create_time',[$start,$end])
            ->sum('money');
        $lists = $rechargeModel->where(['store_id'=>$store_id])
            ->where($typewhere)
            ->whereBetween('create_time',[$start,$end])
            ->order('id','desc')
            ->paginate($limit);
        $data = [
            'list'=>$lists,
            'zhichu'=>$zhichu,
            'shouru'=>$shouru
        ];
        $this->success('',$data);
    }
}
